public class 删除链表的倒数第N个结点 {
    
    public static void main(String[] args) {
        


    }

    public static ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode root = new ListNode(-1);
        root.next = head;
        ListNode pre = root;
        ListNode next, cur;
        while (true) {
            cur = pre.next;
            next = cur;
            for (int i = 1; i <= n; i++) {
                next = next.next;
            }
            if (next == null) {
                break;
            }
            pre = pre.next;
        }
        // 此时的cur指着当前需要删除的节点
        pre.next = cur.next;
        return root.next;

    }


    private static class ListNode {
    
        int val;
        ListNode next;
        public ListNode(int val) {

            this.val = val;
        }
        
    }
}
